package com.jewelry.web.intercepter;

import java.util.Map;

import com.jewelry.common.util.Constants;
import com.jewelry.web.vo.UserSession;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 登录拦截器
 * 
 * @author yangfeng
 * 
 */
public class AuthorityInterceptor extends AbstractInterceptor implements Constants {

	private static final long serialVersionUID = -988986169595320916L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// 取得请求相关的ActionContext实例
		ActionContext ctx = invocation.getInvocationContext();

		String nameSpace = invocation.getProxy().getNamespace();

		Map<String, Object> session = ctx.getSession();
		// 取出名为user的session属性
		Object obj = session.get(Constants.USER_INFO);

		if (obj != null) {
			UserSession userSession = (UserSession) obj;
			// 通过用户类型来判断
			Integer userType = userSession.getUserVO().getUserType();
			
			switch(userType){
			case 1: //管理员
				return invocation.invoke();
			case 2: // 板房 
				if(NAMESPACE_OFFICE.equals(nameSpace)){
					return invocation.invoke();
				}
				break;
			case 3: //工程
				if(NAMESPACE_FACTORY.equals(nameSpace)){
					return invocation.invoke();
				}
				break;
			}
		}
		// 没有登陆，将服务器提示设置成一个HttpServletRequest属性
		Map<String, Object> map = invocation.getInvocationContext()
				.getParameters();
		map.put(Constants.TIP_MSG, Constants.SESSION_TIMEOUT_MSG);
		map.put(Constants.TIP_TYPE, 1);
		return Action.LOGIN;
	}
}
